home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
language
/
embedded
/
simulato
/
v2_3_mc6.tz
/
v2_3_mc6
/
testfiles
/
hex2bin.asm
< prev
next >
Wrap
Assembly Source File
|
1994-05-02
|
5KB
|
117 lines
**** SUBROUTINE HEX2BIN ****
* This routine is a seperate module from the routine "MAIN". It's sole
* purpose is to take the parsed ASCII string passed to it and convert it
* to another ASCII string in the form of an equivalent binary number.
* This task is accomplished by first converting the ASCII string into
* it's correct integer, then testing the negative flag (a passed param.)
* , and finally converting the number into the correct binary ASCII string.
* The parameters must be passed on the stack as shown in the following
* diagram:
*
* |------------------------------------------------|
* | POINTER TO STRING TO BE PARSED(LONGWORD) |
* |------------------------------------------------|
* | POINTER TO PLACE WHERE OUTPUT WILL BE(LONGWORD)|
* |------------------------------------------------|
* | NEGATIVE FLAG(WORD) |
* |------------------------------------------------|
*
****************************
**** RESOURCES ****
* INPUTS: negative flag (held by d3)
* pointer to parsed string (held by a2, last character)
* pointer to desired output location (held by a5)
* OUTPUT: binary string at location pointed to by a5.
*
* REGISTERS: a2 -> pointer to last char of parsed string.
* a5 -> pointer to desired output location
* d2 -> read character from string.
* d3 -> holder of negative flag.
* d4 -> final converted number.
* d5 -> generic counter/holder for multiplication.
********************
SECTION H2B,code ;define section.
XDEF HEX2BIN ;point out the two returns defined here.
HEX2BIN LINK A6,#0 ;link sub.using a6 as fp, w/ wrd local stor.
MOVEM.L A0-A5/D0-D7,-(A7) ;save all registers for restore later.
CLR.L D4 ;clear working total register.
CLR.L D2 ;clear chr. reg. so word mult. ok.
MOVEA.L 8(A6),A2 ;load in param. for parsed string ch.
MOVE.L #$01,D5 ;load w/ hex place holder.
HEX_NEXTCHR MOVE.B (A2),D2 ;read in LSB/next char. of string.
CMPI.B #'$',D2 ;is the last char.
BEQ.S MAKE_BIN_STR ;if so get out. else,
SUBQ.L #1,A2 ;decrement to point to next character.
CMPI.B #'A',D2 ;see if the char. is a # or letter.
BGE.S HEX_ISLETTER ;if the parsed char. is >= it is letter
ANDI.B #$0F,D2 ;else # so strip of top nibble mk #
BRA.S HEX_UPDATE ;go to update.
HEX_ISLETTER SUBI.B #$37,D2 ;converts to correct #.
HEX_UPDATE MULU.W D5,D2 ;put the result in the correct place.
MULU.W #$10,D5 ;update to next place.
ADD D2,D4 ;update working total.
BRA.S HEX_NEXTCHR ;repeat.
MAKE_BIN_STR CLR.L D5 ;clear the generic counter.
MOVE.W 16(A6),D3 ;this is the value of the negative flag
MOVE.L 12(A6),A5 ;get output pointer parameter.
TST.W D3 ;test the flag
BEQ.S HEX_FIRST1 ;if equal not negative
NEG.W D4 ;else take 2's complement of result.
HEX_FIRST1 ROL.W #1,D4 ;get MSB of result.
BCS.S HEX_GOT1 ;if the result was a one handle it
CMPI.W #16,D5 ;is end of word.
BEQ.S HEX_NO1 ;if it is the number was zero.
ADDQ.W #1,D5 ;increment counter.
BRA.S HEX_FIRST1 ;and look for next bit.
HEX_NO1 MOVE.B #'0',(A5)+ ;move $30 to output string.
BRA.W HEX_END ;leave and return.
HEX_GOT1 MOVE.B #'1',(A5)+ ;place the initial one in the string.
CMPI.W #7,D5 ;look for middle.
BNE.S HEX_NOMID ;if not middle continue.
MOVE.B #' ',(A5)+ ;else place a space in the string.
HEX_NOMID ADDQ.W #1,D5 ;increment the counter.
CMPI.W #16,D5 ;see if was the only one.
BGE.W HEX_END ;if it was leave.
HEX_TOEND ROL.W #1,D4 ;get the next bit in the string.
BCS.S HEX_IS1 ;bit is one so place in string.
MOVE.B #'0',(A5)+ ;else put a zero.
BRA.S HEX_SKIP ;skip placing a 1.
HEX_IS1 MOVE.B #'1',(A5)+ ;put 1 in string.
HEX_SKIP CMPI.W #7,D5 ;is the string at its middle.
BNE.S HEX_NOMID2 ;branch
MOVE.B #' ',(A5)+ ;if is place a blank in string.
HEX_NOMID2 CMPI.W #15,D5 ;look for end bit
BEQ.S HEX_END ;leave.
ADDQ.W #1,D5 ;else increment counter
BRA.S HEX_TOEND ;and continue processing.
HEX_END MOVE.B #0,(A5) ;place terminator in string.
MOVEM.L (A7)+,D0-D7/A0-A5 ;restore registers.
UNLK A6 ;restore stack
RTS ;return.
END